﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Text;
using Quartz;
using Wicresoft.RDW.Model;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Service
{
    public class RequestMonitor : IJob
    {
        EventLog log = new EventLog();
        public RequestMonitor()
        {
            if (!EventLog.SourceExists("source"))
            {
                EventLog.CreateEventSource("source", "RequestMonitorLog");
            }
            log.Source = "source";
        }
        public void Execute(JobExecutionContext context)
        {
            OperateOverTimeUnApprovedRequst();
           
        }
        private void OperateOverTimeUnApprovedRequst()
        {
            
            try
            { 
                log.WriteEntry(DateTime.Now + "----------execute db query");
                var settings = SysSettingPresent.ApproveWaitingDay;
                double day = 0d;
                if (string.IsNullOrEmpty(settings))
                {
                    day = 2d;
                }
                else
                {
                    day = double.Parse(settings);
                }
                log.WriteEntry(DateTime.Now + "----------get settings ok");
                
                log.WriteEntry(DateTime.Now + "----------start get request list");

                var terminalTime = DateTime.Now.AddDays(-day);
                var requestList = RequestPresent.GetList(x => (terminalTime>= x.CreateTime&& x.Status == (int)RequestStatus.Pending));
                var dns = ConfigurationManager.AppSettings["DNS"];
                var port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                foreach (var request in requestList)
                {

                    log.WriteEntry(DateTime.Now + "----------请求编号：" + request.RequestId + "/" + request.CreateTime);
                    Approve(request.RequestId, dns,port);
                }
                
                log.WriteEntry(DateTime.Now + "job execute OK");

            }
            catch (Exception e)
            {
               
                log.WriteEntry(DateTime.Now + e.Message+" \r\n execute job exception");
            }

        }
        private void Approve(int RequestId, string dns,int port)
        {
            var content = new StringBuilder();
            var userNameCN = string.Empty;
            var userNameEN = string.Empty;
            var linkAddress = string.Empty;
            var linkEmail = string.Empty;
            var linkPhone = string.Empty;
            var requst = RequestPresent.GetById(RequestId);
            var requstLab = LabPresent.GetById(requst.LabId);
            if (requstLab == null) requstLab = new Lab();
            var requestUserEmail = RequestPresent.GetRequestUserEmail(requst.RequestUserId);

            RequestPresent.GetUserNameById(requst.RequestUserId, out userNameCN, out userNameEN);
            linkAddress = RequestPresent.GetLinkAddressByRequestId(requst.RequestId, dns, true,port, Language.EN,false);
            RequestPresent.GetEmailPhoneById(RequestId, out linkEmail, out linkPhone);

            content.Append(userNameEN + "^" + requstLab.Name + "^" + requst.RequestDate.Year + "^" + requst.RequestDate.Month + "^"
                    + requst.RequestDate.Day + "^" + linkAddress + "^" + linkEmail + "^" + linkPhone + "^");
            linkAddress = RequestPresent.GetLinkAddressByRequestId(requst.RequestId, dns, true,port, Language.CN,false);
            content.Append(userNameCN + "^" + requstLab.Name + "^" + requst.RequestDate.Year + "^" + requst.RequestDate.Month + "^"
                + requst.RequestDate.Day + "^" + linkAddress + "^" + linkEmail + "^" + linkPhone);
            RequestPresent.UpdateRequestById(RequestId, (int)(RequestStatus.Approved), string.Empty);
            var ccList = RequestPresent.GetLabManagerAndSiteManagerEmail(requstLab.LabId, requstLab.LocationId);
            if (ccList == null)
            {
                ccList = new List<string>();
            }
            var feedBack = new Feedback();
            feedBack.IsPublished = true;
            feedBack.RequestId = RequestId;
            feedBack.Status = (int)FeedbackStatus.Active;
            FeedbackPresent.Create(feedBack);
            MailPresent.SendMail(EmailTemplate.ApprovedReminder, content.ToString(), requestUserEmail, ccList);
        }
    }
}
